<?php
namespace BABA\Omnipay\Components;

use Illuminate\Support\Facades\DB;
use BABA\Omnipay\Models\Settings;
use Omnipay\Omnipay;
use Illuminate\Support\Facades\Event;
use Lang;
use Url;
use Request;

class Pay extends \Cms\Classes\ComponentBase
{
    public function componentDetails()
    {
        return [
            'name' => Lang::get('baba.omnipay::lang.components.pay.name'),
            'description' => Lang::get('baba.omnipay::lang.components.pay.description'),
        ];
    }

    public function defineProperties()
    {
        return [
            'table' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.table.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.table.description'),
                'default'           => 'baba_ebill_order',
                'type'              => 'string',
                'validationPattern' => '^[a-z_]+$',
                'validationMessage' => Lang::get('baba.omnipay::lang.components.pay.table.validation'),
            ],
            'PSP' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.PSP.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.PSP.description'),
                'default'           => 'Mollie',
                'type'              => 'dropdown',
                'options'           => [
                    'Mollie'            => 'Mollie',
                    'PayPal_Express'    => 'PayPal_Express'
                ]
            ],
            'ApiKey' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.ApiKey.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.ApiKey.description'),
                'type'              => 'string',
                'group'             => 'Mollie'
            ],

            'Username' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.Username.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.Username.description'),
                'type'              => 'string',
                'group'             => 'PayPal_Express'
            ],
            'Password' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.Password.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.Password.description'),
                'type'              => 'string',
                'group'             => 'PayPal_Express'
            ],
            'Signature' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.Signature.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.Signature.description'),
                'type'              => 'string',
                'group'             => 'PayPal_Express'
            ],
            'TestMode' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.TestMode.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.TestMode.description'),
                'default'           => false,
                'type'              => 'checkbox',
                'group'             => 'PayPal_Express'
            ],

            'returnUrl' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.returnUrl.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.returnUrl.description'),
                'default'           => Url::to('/').'/payresult',
                'type'              => 'string'
            ],
            'cancelUrl' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.cancelUrl.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.cancelUrl.description'),
                'default'           => Url::to('/').'/payresult',
                'type'              => 'string'
            ],
            'notifyUrl' => [
                'title'             => Lang::get('baba.omnipay::lang.components.pay.notifyUrl.title'),
                'description'       => Lang::get('baba.omnipay::lang.components.pay.notifyUrl.description'),
                'default'           => Url::to('/').'/payresult',
                'type'              => 'string'
            ]
        ];
    }

    function onRun()
    {
        // get order from the DB based on ID
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if($id)
        {
            $booking = Db::table( $this->property('table') ) // e.g. briddle_book_payment
            ->select(
                Settings::get('map_amount','amount'),// e.g. 100.00
                Settings::get('map_currency','currency'),// e.g. EUR
                Settings::get('map_description','description') // e.g. Chesterfield sofa
            )
            ->where([
                [Settings::get('map_id','id'),$id]
            ])
            ->first();

            $map_amount = Settings::get('map_amount','amount');
            $map_currency = Settings::get('map_currency','currency');
            $map_description = Settings::get('map_description','description');

            // Setup PSP
            $gateway = Omnipay::create( $this->property('PSP') );// e.g. Mollie

            switch($this->property('PSP'))
            {
                case "Mollie":
                    $gateway->setApiKey( $this->property('ApiKey') ); // e.g. test_12345 as used by Mollie
                break;
                case "PayPal_Express":
                    $gateway->setUsername( $this->property('Username') );
                    $gateway->setPassword( $this->property('Password') );
                    $gateway->setSignature( $this->property('Signature') );
                    $gateway->setTestMode( $this->property('TestMode') );// e.g. true
                break;
            }



            $response = $gateway->purchase(
            [
                 "amount" => $booking->$map_amount
                ,"currency" => $booking->$map_currency
                ,"description" => $booking->$map_description
                ,"returnUrl" => $this->property('returnUrl') . "/?id=" . $id // e.g. https://www.mysite.com
                ,"cancelUrl" => $this->property('returnUrl') . "/?id=" . $id // e.g. https://www.mysite.com
                ,"notifyUrl" => $this->property('returnUrl') . "/?id=" . $id // e.g. https://www.mysite.com
            ]
        	)->send();

        	// Get a reference to the order
        	$transactionReference = $response->getTransactionReference();
        	Db::table( $this->property('table') ) // e.g. briddle_book_payment
            ->where(
                Settings::get('map_id','id'), $id
            )->update(
                [Settings::get('map_paymentid','paymentid') => $transactionReference]
            );

            //Event
            Event::fire('baba.omnipay.pay',$booking);

        	// Process response
        	if ($response->isSuccessful())
            {
        	    // Payment was successful: not used here because we redirect
        	}
            elseif ($response->isRedirect())
            {
        		// Redirect to offsite payment gateway
        	    $response->redirect();
        	}
            else
            {
        	    // Payment failed
        	    // echo $response->getMessage();
        	}
        }
    }
}
